﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace 第九次课堂作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //点击查找按钮，查找相应的手机号和邮箱号
        private void button2_Click(object sender, EventArgs e)
        {
            myClear();
            string url = MyURL.Text;
            System.Net.WebClient client = new System.Net.WebClient();
            client.Encoding = Encoding.UTF8;
            try
            {
                byte[] data = client.DownloadData(url);
                string msg = System.Text.Encoding.Default.GetString(data);
                string phonePattern = "(phonenumber|phoneNumber|手机号码):[0-9]{11}";
                string emailPattern = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
                addPhone(phonePattern, msg);
                addEmail(emailPattern, msg);
            }
            catch (Exception a)
            {
                MessageBox.Show(a.Message);
            }
        }

        //添加邮箱
        private void addEmail(string emailPattern, string msg)
        {
            Regex rx = new Regex(emailPattern);
            MatchCollection mc = rx.Matches(msg);
            foreach (Match mt in mc)
            {
                MyEmail.Text += (mt.ToString() + "\r\n");
            }
            if (MyEmail.Text == "")
            {
                MyEmail.Text += "未找到邮箱";
            }
        }

        //添加电话
        private void addPhone(string phonePattern, string msg)
        {
            Regex rx = new Regex(phonePattern);
            MatchCollection mc = rx.Matches(msg);
            foreach (Match mt in mc)
            {
                string[] temp = mt.ToString().Split(':');
                MyPhone.Text += (temp[1] + "\r\n");
            }
            if (MyPhone.Text == "")
            {
                MyPhone.Text += "未找到手机号码";
            }
        }

        //清空文本框
        private void myClear()
        {
            MyPhone.Text = "";
            MyEmail.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MyURL.Text = "empty.html";
        }
    }
}
